
// NodeJs  链接  MongoDB   数据库
//Mongoose  连接数据库
// Mongoose是在node.js环境下对mongodb进行便捷的对象模型工具
const mongoose = require('mongoose')

const hostname = "localhost"
const port = 27107
const dbname = "wh2114"
const user = "?"
const pwd = "?"

const CONN_DB_URL = `mongodb://${hostname}:${port}/${dbname}`

// 有用户名和密码写
// const CONN_DB_URL = `mongodb://${user}:${pwd}@${hostname}:${prot}/${dbname}`

mongoose.connect(CONN_DB_URL, err => {
    if (err) {
        console.log("数据库链接失败-fail");
    } else {
        console.log("数据库链接成功 -success");
    }
})

const connection = mongoose.connection;

connection.on("open", () => {
    console.log("开始打开数据库链接");
})

connection.on("connection", () => {
    console.log("disconnected - 数据库断开连接");
})

connection.on("error", err => {
    console.log("数据库异常 --- " + err);
})